﻿using Abp.Domain.Repositories;
using Abp.Organizations;
using AppFramework.DinosaursParkMes.TicketTask.DTO;
using AppFramework.TicketTask;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.TicketTask
{
    /// <summary>
    /// 派工管理
    /// </summary>
    public class SendWorkinfoAppService : AppFrameworkAppServiceBase, ISendWorkinfoAppService
    {
        private readonly IRepository<SendWorkinfo> repository;
        private readonly IRepository<TeamorGroupinfo> teamorGroupinforepository;
        private readonly IRepository<OrganizationUnit,long> organizationUnitrepository;
        private readonly IRepository<TicketTaskinfo> ticketTaskinforepository;

        public SendWorkinfoAppService(IRepository<SendWorkinfo> repository, IRepository<TeamorGroupinfo> teamorGroupinforepository = null, IRepository<OrganizationUnit, long> organizationUnitrepository = null, IRepository<TicketTaskinfo> ticketTaskinforepository = null)
        {
            this.repository = repository;
            this.teamorGroupinforepository = teamorGroupinforepository;
            this.organizationUnitrepository = organizationUnitrepository;
            this.ticketTaskinforepository = ticketTaskinforepository;
        }
        /// <summary>
        /// 派工
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost,Route("InsertSendWork")]
        public async Task<int> InsertSendWork(SendWorkRequestDTO dto)
        {
            var list = ticketTaskinforepository.GetAll().Where(m=>m.Missionnumber==dto.Missionnumber).FirstOrDefault();
            
            var sendwork = ObjectMapper.Map<SendWorkinfo>(dto);
            list.States = TicketTaskEnum.HaveBeenIssued;
            await repository.InsertAsync(sendwork);
            return 1;
        }
        /// <summary>
        /// 派工信息详情
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet,Route("SendWorkSelect")]
        public async Task<SendWorkSelectResponseDTO> SendWorkSelect(string code)
        {
            var tick = ticketTaskinforepository.GetAll();
            var sendwork = from a in repository.GetAll()
                           join b in teamorGroupinforepository.GetAll() on a.Classid equals b.Id
                           join c in organizationUnitrepository.GetAll() on a.Departmentid equals c.Id
                           select new SendWorkSelectResponseDTO
                           {
                               Classid= a.Classid,
                               Classprincipal= a.Classprincipal,
                               Departmentid= a.Departmentid,
                               Departmentname= a.Departmentname,
                               Missionnumber= a.Missionnumber,
                               Othermembers= a.Othermembers,
                               Taskremark= a.Taskremark,
                               Qualitytestingremark= a.Qualitytestingremark,
                               Classname=b.Classname,
                               SectionName=c.DisplayName,
                               CtrationTime=a.CreationTime
                           };
            
            return sendwork.FirstOrDefault();
        }
    }
}
